package com.cubeofnine.sugar.business;

import com.cubeofnine.sugar.service.DatabaseService;
import com.cubeofnine.sugar.service.SugarCrmService;
import com.cubeofnine.sugar.service.element.DatabaseElement;
import com.cubeofnine.sugar.service.element.ErrorValue;
import com.cubeofnine.sugar.service.element.LoginResult;

import android.content.Context;
import android.util.Log;

public class LoginBusiness {
	private Context context = null;
	private SugarCrmService client = null;

	public static String getClassName() {
		return LoginBusiness.class.getName();
	}

	public LoginBusiness(Context context) throws Exception {
		Log.d(getClassName(), "context = " + context);
		try {
			this.context = context;
		} catch (Exception e) {
			throw e;
		}
	}

	public boolean checkUsernamePasswordFormat(String username, String password)
			throws Exception {
		Log.i(getClassName(), "checkUsernamePasswordFormat() : Start");
		Log.d(getClassName(), "username = " + username);
		Log.d(getClassName(), "password = " + password);
		boolean result = true;

		if ("".equals(username) || "".equals(password)) {
			return false;
		} else if ((!"ADMIN".equalsIgnoreCase(username) && username.length() < 8)
				|| password.length() < 8) {
			return false;
		} else {
			int ch;
			for (int i = 0; i < username.length(); i++) {
				ch = (int) username.charAt(i);
				if (ch >= (int) 'A' && ch <= (int) 'Z') {
					continue;
				} else if (ch >= (int) 'a' && ch <= (int) 'z') {
					continue;
				} else if (ch >= (int) '0' && ch <= (int) '9') {
					continue;
				} else if (ch == (int) '@' || ch == (int) '_'
						|| ch == (int) '-' || ch == (int) '.') {
					continue;
				} else {
					return false;
				}
			}
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "checkUsernamePasswordFormat() : End");
		return result;
	}

	public DatabaseElement getDefaultValue() throws Exception {
		Log.i(getClassName(), "getDefaultValue() : Start");

		DatabaseService business = new DatabaseService(context);
		DatabaseElement result = null;
		try {
			result = business.getDatabaseValue();
		} catch (Exception e) {
			throw e;
		} finally {
			business.closeDatabase();
		}

		Log.i(getClassName(), "getDefaultValue() : End");
		return result;
	}

	public boolean saveValue(String username, String password,
			String sugarcrmURL, boolean rememberPassword,
			boolean automaticLogin, String sessionId, boolean isUserAdmin)
			throws Exception {
		Log.i(getClassName(), "saveValue() : Start");
		Log.d(getClassName(), "username = " + username);
		Log.d(getClassName(), "password = " + password);
		Log.d(getClassName(), "sugarcrmURL = " + sugarcrmURL);
		Log.d(getClassName(), "rememberPassword = " + rememberPassword);
		Log.d(getClassName(), "automaticLogin = " + automaticLogin);
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "isUserAdmin = " + isUserAdmin);

		DatabaseService business = new DatabaseService(context);
		boolean result = false;
		try {
			business.delete(-1);

			if (!rememberPassword) {
				password = "";
				automaticLogin = false;
			}

			result = business.insert(username, password, sugarcrmURL,
					rememberPassword, automaticLogin, sessionId, isUserAdmin);
		} catch (Exception e) {
			throw e;
		} finally {
			business.closeDatabase();
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "saveValue() : End");
		return result;
	}

	public void connectSugarCrmWebService(String sugarUrl) throws Exception {
		Log.i(getClassName(), "connectSugarCrmWebService() : Start");
		Log.d(getClassName(), "sugarUrl = " + sugarUrl);

		try {
			client = new SugarCrmService(sugarUrl);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "connectSugarCrmWebService() : End");
	}

	public LoginResult login(String username, String password) throws Exception {
		Log.i(getClassName(), "login() : Start");
		Log.d(getClassName(), "username = " + username);
		Log.d(getClassName(), "password = " + password);
		LoginResult result = null;

		try {
			result = client.getSessionId(username, password);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "login() : End");
		return result;
	}

	public ErrorValue logout(String sessionId) throws Exception {
		Log.i(getClassName(), "logout() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		ErrorValue result = null;

		try {
			result = client.logout(sessionId);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "logout() : End");
		return result;
	}

	public boolean isUserAdmin(String sessionId) {
		Log.i(getClassName(), "isUserAdmin() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);

		boolean result = (1 == client.isUserAdmin(sessionId));

		Log.i(getClassName(), "isUserAdmin() : End");
		return result;
	}

	public boolean unAutomaticLogin() throws Exception {
		Log.i(getClassName(), "unAutomaticLogin() : Start");
		DatabaseService business = new DatabaseService(context);
		boolean result = false;
		try {
			result = business.updateAutomaticLogin(false);
		} catch (Exception e) {
			throw e;
		} finally {
			business.closeDatabase();
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "unAutomaticLogin() : End");
		return result;
	}
}
